package com.xuxueli.crawler.proxy.strategy;

import com.xuxueli.crawler.proxy.ProxyMaker;

import java.net.Proxy;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 轮询策略代理获取
 *
 * @author xuxueli 2017-11-07 20:06:54
 */
public class RoundProxyMaker extends ProxyMaker {

    private final AtomicInteger count = new AtomicInteger(0);
    private final static int MAX_ROUND = 100000;

    @Override
    public Proxy make() {
        if (super.proxyList==null || super.proxyList.size()==0) {
            return null;
        }

        if (super.proxyList.size() == 1) {
            return super.proxyList.get(0);
        }

        int countVal = count.incrementAndGet();
        if (countVal > MAX_ROUND) {
            countVal = 0;
            count.set(countVal);
        }

        return super.proxyList.get(countVal % super.proxyList.size());
    }

}
